package 哈希.最长连续序列128;

import utils.Utils;

import java.util.HashSet;
import java.util.Set;

public class Solution {
    public static int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet();
        for(int num : nums){
            set.add(num);
        }
        int res = 0;

        for(Integer num : set){
            if(!set.contains(num - 1)){
                int currentNum = num;
                int currIndex = 1;

                while (set.contains(currentNum + 1)){
                    currentNum++;
                    currIndex++;
                }

                res = Math.max(res,currIndex);
            }

        }

        return res;

    }

    public static void main(String[] args) {
        int[] nums = {100,4,200,1,3,2};
        Utils.printInt(longestConsecutive(nums));
    }
}
